package com.springboot.learn_web_rabbitmq.demo2;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 测试消息持久化功能
 * 需要在声明队列时声明为持久化，消息也设置为持久化（MessageProperties.PERSISTENT_TEXT_PLAIN）
 */
public class Producer {
    private static final String QUEUE_NAME="QUEUE_TEST";
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory=new ConnectionFactory();
        Connection connection=factory.newConnection();
        Channel channel=connection.createChannel();
        channel.queueDeclare(QUEUE_NAME,true,false,false,null);
        for(int i=0;i<100;i++){
            String messgae="hello rabbitmq~~ "+" "+i;
            channel.basicPublish("",QUEUE_NAME,MessageProperties.PERSISTENT_TEXT_PLAIN,messgae.getBytes("UTF-8"));
        }
        channel.close();
        connection.close();
    }
}
